package org.toonWatch

class HomeController {

    def springSecurityService
    def voucherService

    def index = { 
        def adminRole = Role.findByAuthority('ROLE_ADMIN')
        if (springSecurityService.isLoggedIn()){
            if (springSecurityService.currentUser.authorities.contains(adminRole)){
                redirect (uri: "/adminHome.gsp")
            }
            else{
                def user = springSecurityService.currentUser
                session.user = user
                session.userVoucher = 0
                
                /*
                
                def sum = 0
                user.voucher.each(){
                    sum += it.amount
                }
                sum -= user.voucherUsed
                session.userVoucher = sum
                println sum
                
                */
               
                session.userVoucher = voucherService.getAmount(user.id)
                
                    
                redirect (uri: "/home.gsp")
            }
        }
        else{
            redirect (action: "login")
        }
    }
}
